package com.gzhryc.shared_device.oem.micro.iot;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import com.gzhryc.common.ClassTools;
import com.gzhryc.common.StringTools;
import com.gzhryc.common.json.JsonTools;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.shared_device.base.enums.EOperatorType;
import com.gzhryc.shared_device.base.models.OperateInfo;
import com.gzhryc.shared_device.bxscn.code.BXSCNNetworkEventListener;
import com.gzhryc.shared_device.bxscn.core.BXSCNConstants;
import com.gzhryc.shared_device.bxscn.core.protocol.v1.V1_BXSCN_IOTClientEventListener;
import com.gzhryc.shared_device.cdb.code.CDBNetworkEventListener;
import com.gzhryc.shared_device.cdb.core.CDBConstants;
import com.gzhryc.shared_device.cdb.core.protocol.v1.V1_CDB_IOTClientEventListener;
import com.gzhryc.shared_device.cdcz.code.CDCZNetworkEventListener;
import com.gzhryc.shared_device.cdcz.core.CDCZConstants;
import com.gzhryc.shared_device.cdcz.core.protocol.v1.V1_CDCZ_IOTClientEventListener;
import com.gzhryc.shared_device.cdcz.core.protocol.v2.V2_CDCZ_IOTClientEventListener;
import com.gzhryc.shared_device.iot.code.dao.db.IOTServerNode;
import com.gzhryc.shared_device.iot.code.services.IOTServerNodeService;
import com.gzhryc.shared_device.iot.core.IOTClient;
import com.gzhryc.shared_device.iot.core.IOTClientEventListener;
import com.gzhryc.shared_device.iot.core.IOTNodeEntity;
import com.gzhryc.shared_device.iot.core.enums.EIOTType;
import com.gzhryc.shared_device.iot.core.hryc.HRYCSmartMqttIotClient;
import com.gzhryc.shared_device.iot.core.hryc.models.HRYCIOTConfig;
import com.gzhryc.shared_device.oem.code.device.BXSCNCacheManager;
import com.gzhryc.shared_device.oem.code.device.CDBCacheManager;
import com.gzhryc.shared_device.oem.code.device.CDCZCacheManager;

public class IOTServerNodeFactory {

    static Logger log = Logger.getLogger(IOTServerNodeFactory.class);

    static IOTServerNodeFactory me;

    public static IOTServerNodeFactory self(){
        if(me == null){
            me = new IOTServerNodeFactory();
        }
        return me;
    }

    private ConcurrentHashMap<Long, IOTClient> iotClientMap = new ConcurrentHashMap<>();

    public void initial() {
        List<IOTServerNode> iotServerNodeList = IOTServerNodeService.self().findAllUnable();
        if (iotServerNodeList != null && iotServerNodeList.size() > 0) {
            for (IOTServerNode node : iotServerNodeList) {
                String linkConfig = node.getLinkConfig();
                if (StringTools.isNotBlank(linkConfig)) {
                    IOTNodeEntity iotNodeEntity = ClassTools.copy(new IOTNodeEntity(), node);

                    IOTClientEventListener listener = null;
                    OperateInfo operateInfo = new OperateInfo(MicroConstants.PROGRAM_TYPE,MicroConstants.PROGRAM_TYPE_NAME);
                    operateInfo.setOperator(EOperatorType.System.index(),"IOT初始化服务");

                    String jdbcKey = null;
                    if (CDCZConstants.ProtocolTypeV1.equals(iotNodeEntity.getProtocolType())) {
                        listener = new V1_CDCZ_IOTClientEventListener(IOTLogServer.self(), new CDCZNetworkEventListener(jdbcKey,
                                IOTLogServer.self(), new CDCZBusinessEventListener(), CDCZCacheManager.self(), operateInfo));
                    } else if (CDCZConstants.ProtocolTypeV2.equals(iotNodeEntity.getProtocolType())) {
                        listener = new V2_CDCZ_IOTClientEventListener(IOTLogServer.self(), new CDCZNetworkEventListener(jdbcKey,
                                IOTLogServer.self(), new CDCZBusinessEventListener(), CDCZCacheManager.self(), operateInfo));
                    } else if (CDBConstants.ProtocolTypeV1.equals(iotNodeEntity.getProtocolType())) {
                        listener = new V1_CDB_IOTClientEventListener(IOTLogServer.self(), new CDBNetworkEventListener(jdbcKey,
                                IOTLogServer.self(), new CDBBusinessEventListener(), CDBCacheManager.self(), operateInfo));
                    }else if (BXSCNConstants.ProtocolTypeV1.equals(iotNodeEntity.getProtocolType())) {
                        listener = new V1_BXSCN_IOTClientEventListener(IOTLogServer.self(), new BXSCNNetworkEventListener(jdbcKey,
                                IOTLogServer.self(), new BXSCNBusinessEventListener(), BXSCNCacheManager.self(), operateInfo));
                    }
                    if (listener != null) {
                        if (EIOTType.HRYC_SMART.index().equals(iotNodeEntity.getIotType())) {
                            HRYCIOTConfig hrycIOTConfig = JsonTools.fromJson(linkConfig, HRYCIOTConfig.class);
                            hrycIOTConfig.setIotId(iotNodeEntity.getId());
                            HRYCSmartMqttIotClient client = new HRYCSmartMqttIotClient(hrycIOTConfig, iotNodeEntity, listener);
                            client.run();
                            this.iotClientMap.put(node.getId(), client);
                            log.info("启动浩睿洋诚IOT服务，ID：" + node.getId() + "，配置信息：" + linkConfig);
                        }
                    } else {
                        log.error("{{0}}IOT节点未找到IOT事件监听者，协议：{{1}}", node.getId(), node.getProtocolType());
                    }
                }
            }
        }
    }

    public IOTClient getIOTClient(Long iotId){
        if(iotId != null) {
            return iotClientMap.get(iotId);
        }
        return null;
    }
}
